package cn.itcast.controller.cargo;

import cn.itcast.domain.cargo.Packing;
import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("cargo/packing")
public class PackingController extends BaseController {
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;
    @Reference
    private PackingService packingService;
    @RequestMapping(value = "/toAdd",name = "跳转保存装箱单页面")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String id){
        String[] split = id.split(",");
        Export export1 = exportService.findById(split[0]);
        String consignee = export1.getConsignee();
        String shipmentPort = export1.getShipmentPort();
        String destinationPort = export1.getDestinationPort();
        Packing packing = new Packing();
        String exportNos = null;
        double totalVolume = 0;
        double netWeights=0;
        double grossWeights = 0;
        for (String s : split) {
            Export export = exportService.findById(s);
            Integer state = export.getState();
            if (state!=2){
                throw new RuntimeException("货物未报运");
            }
            if (!(consignee.equals(export.getConsignee())||shipmentPort.equals(export.getShipmentPort())||destinationPort.equals(export.getDestinationPort()))){
                throw new RuntimeException("装运港、目的港、收货人不一致");
            }
            List<ExportProduct> exportProducts = exportProductService.findByExportId(s);
            for (ExportProduct exportProduct : exportProducts) {
                netWeights += exportProduct.getNetWeight();
            }
            exportNos+=export.getLcno()+" ";
            totalVolume += export.getMeasurements();
            grossWeights += export.getGrossWeights();
            exportNos += export.getLcno();
        }
        packing.setExportIds(id);
        packing.setExportNos(exportNos);
        packing.setNetWeights(netWeights);
        packing.setTotalVolume(totalVolume);
        packing.setGrossWeights(grossWeights);
        packing.setPackingMoney(grossWeights*520);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-add";
    }
    @RequestMapping(value = "/edit",name = "保存装箱单数据")
    public String edit(Packing packing){

        if(StringUtils.isEmpty(packing.getPackingListId())){
            packing.setPackingListId(UUID.randomUUID().toString());
            packing.setCompanyId(getCompanyId());
            packing.setCompanyName(getCompanyName());
            packing.setState(0);
            packingService.save(packing);
        }else{
            packingService.update(packing);
        }
        return "redirect:/cargo/packing/list.do";
    }
    @RequestMapping(value = "/list",name = "展示装箱列表数据")
    public String findAll(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Packing> pageInfo = packingService.findAll(getCompanyId(),page,pageSize);
        request.setAttribute("page",pageInfo);


        return "cargo/packing/packing-list";
    }
    @RequestMapping(value = "/delete",name = "删除某个装箱管理")
    public String delete(String id){
        packingService.delete(id);
        return "redirect:/cargo/packing/list.do";
    }
    @RequestMapping(value = "/packlistExcel",name = "下载装箱单")
    public void packlistExcel(String id){
        //需要装箱单的数据
         Packing packing=packingService.findById(id);


    }
}

